#!/bin/bash
#function create_kafka_topic {
#    $KAFKA_HOME/bin/kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic $1
#}

function send_messages_to_kafka {
    msg=$(generator_message)
    echo -e $msg | $KAFKA_HOME/bin/kafka-console-producer.sh --broker-list kafka:9092 --topic $TOPIC
#    echo -e $msg
}
function rand {
    min=$1
    max=$(($2-$min+1))
    num=$(date +%s)
    echo $(($num%$max+$min))
}
function generator_message {
    uid=$(rand 1 20);
    product_id=$(rand 1 999);
    timestamp=`date '+%s'`;
    lib=$(rand 0 4);
    channel=$(rand 0 3);

    lib_var=${lib_arr[$lib]}
    channel_var=${channel_arr[$channel]}

    echo $template |sed  "s/var1/$uid/g" | sed "s/var2/$timestamp/g" | sed "s/var3/$lib_var/g" | sed "s/var4/$channel_var/g" | sed "s/var5/$product_id/g"
#    msg=$uid","$product_id","$timestamp","$action;
#    echo $msg
}

TOPIC="event"
#create_kafka_topic $TOPIC

OLD_IFS=$IFS
export IFS=","

lib_sentence='"js", "Android", "iOS", "MiniProgram", "AliPayMini"'
channel_sentence2='1, 2, 3, 4'
lib_arr=($lib_sentence)
channel_arr=($channel_sentence2)

IFS="$OLD_IFS"
IFS=";"

template='{"distinct_id":var1, "time":var2, "lib":var3, "ty_channel":var4, "product_id":var5}'
#template='{"distinct_id":"var1", time:var2, lib:var3, ty_channel:var4, product_id:var5}'


#while true
for i in {1..30}
do
 send_messages_to_kafka
 sleep 0.3
done
